<?php

namespace App\Http\Controllers\Api\v1;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Services\qikan;
use App\Models\bookDetail;
use App\Models\qikan as qikan_model;
use App\Models\booklist;
use Illuminate\Support\Facades\Cache;

class IndexController extends Controller
{
    public function list(booklist $book_list, qikan $qikan, Request $Request, $id)
    {
        //传递book_id 那么我就要通过book_id
        //1. 判断是否是最新的期刊，是最新的就返回缓存
        //2. 不是最新的就获取最新的期刊，并缓存起来，输出
        //无法判断最新的期刊，所以只能通过缓存处理

        //测试清除数据使用
        // Cache::flush();
        // Cache::forget('3542066a-b9ec-4470-b057-50e64c8c6e87');

        $data = Cache::rememberForever($id, function() use ($book_list, $id, $qikan){
            $data_list = $book_list
                         ->where('book_id', '=', trim($id))
                         ->get();

            if ($data_list->isEmpty()) {
                //需要请求接口
                $qikan->getQikanListById($id);
                if ($qikan->getApiData()->Code == 1) {
                    //转换成数据库需要的字段
                    $qikan->toBookListFields();
                    //批量插入数据库
                    $book_list->insert($qikan->getInsertData());

                    $data_list = $qikan->getApiData()->Data; //obj
                }else{
                    return false;
                }
            }else{
                // $qikan->setApiData($data_list);
                $qikan->toQiKanNumOutPut($data_list); //array
                $data_list = $qikan->getOutPutData();
            }
            return $data_list;
        });
        if ($data) {
            $ret['Code'] = 1;
            $ret['Message'] = null;
            $ret['Data'] = $data;
        }else{
            $ret['Code'] = 1001;
            $ret['Message'] = '暂无数据';
            $ret['Data'] = [];
        }

        return $ret;
    }
    //获取某期书刊
    public function book(bookDetail $book, qikan $qikan, $id, $year, $month)
    {
        $key = $id . '-' . $year . '-' . $month;
        $data = Cache::rememberForever($key, function() use ($book, $id, $qikan, $year, $month){
            $data_list = $book
                         ->where('book_list_id', '=', trim($id))
                         ->get();

            if ($data_list->isEmpty()) {
                //需要请求接口
                $qikan->getCatalogListById($id, $year, $issue);
                if ($qikan->getApiData()->Code == 1) {
                    //转换成数据库需要的字段
                    $qikan->toBookDetailFields($id);
                    //批量插入数据库
                    $book->insert($qikan->getInsertData());

                    $data_list = $qikan->getApiData()->Data; //obj
                }else{
                    return false;
                }
            }else{
                // $qikan->setApiData($data_list);
                $qikan->toQiKanContentListOutPut($data_list); //array
                $data_list = $qikan->getOutPutData();
            }
            return $data_list;
        });

        if ($data) {
            $ret['Code'] = 1;
            $ret['Message'] = null;
            $ret['Data'] = $data;
        }else{
            $ret['Code'] = 1001;
            $ret['Message'] = '暂无数据';
            $ret['Data'] = [];
        }
        return $ret;
    }
    //获取文章详情
    public function article(bookDetail $book, qikan $qikan, $id)
    {
        $data = Cache::rememberForever($id, function() use ($id, $qikan, $book){

            $data_list = $book
                        ->where('article_id', $id)
                        ->whereNotNull('article_content')
                        ->get();
            //结果为空
            if ($data_list->isEmpty()) {
                //需要请求接口
                $qikan->getContentById($id);

                if ($qikan->getApiData()->Code == 1) {
                    //转换成数据库需要的字段
                    $qikan->toBookDetailInput();
                    //todo 条件更新
                    $test = $book
                    ->where('article_id', $id)
                    ->update($qikan->getInsertData());

                    $data_list = $qikan->getApiData()->Data; //obj
                }else{
                    return false;
                }
            }else{
                // $qikan->setApiData($data_list);
                $qikan->toQikanContentOutPut($data_list); //array
                $data_list = $qikan->getOutPutData();
            }
            return $data_list;
        });

        if ($data) {
            $ret['Code'] = 1;
            $ret['Message'] = null;
            $ret['Data'] = $data;
        }else{
            $ret['Code'] = 1001;
            $ret['Message'] = '暂无数据';
            $ret['Data'] = [];
        }
        return $ret;
    }
}
